package service.impl;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import model.Schedule;
import model.Station;
import model.Train;
import service.ScheduleService;
import dao.ScheduleDAO;
import dao.StationDAO;
import dao.TrainDAO;

@Stateless
public class ScheduleServiceImpl implements ScheduleService {

	@EJB
	private ScheduleDAO scheduleDAO;

	@EJB
	private StationDAO stationDAO;

	@EJB
	private TrainDAO trainDAO;

	@PersistenceContext
	private EntityManager em;

	@Override
	public List<Schedule> findAll() {
		return scheduleDAO.findAll();
	}

	@Override
	public Schedule findById(Integer id) {
		return scheduleDAO.find(id);
	}

	public String save(Schedule schedule) {
		Train train = trainDAO.findByNumber(schedule.getTrain()
				.getTrainNumber());
		if (train == null) {
			return "Invalid train information, no such train number in database";
		}
		Station station1 = stationDAO.findByName(schedule.getStation1()
				.getStationName());
		if (station1 == null) {
			return "Invalid departure station information, no such station name in database";
		}
		Station station2 = stationDAO.findByName(schedule.getStation2()
				.getStationName());
		if (station2 == null) {
			return "Invalid departure station information, no such station name in database";
		}
		List<Schedule> sched = scheduleDAO.findIsSchedulePossible(train,
				schedule.getDateOfDeparture(), schedule.getDateOfArrival());
		if (sched != null) {
			if (!sched.isEmpty()) {
				return "The train already has schedule in this time period";
			}		
		}
		schedule.setTrain(train);
		schedule.setStation1(station1);
		schedule.setStation2(station2);
		schedule.setDateOfArrival(schedule.getDateOfArrival());
		schedule.setDateOfDeparture(schedule.getDateOfDeparture());
		scheduleDAO.save(schedule);
		return "ok";
	}

	public void delete(Schedule schedule) {
		scheduleDAO.delete(schedule);
	}

	@Override
	public String update(Schedule temp, Schedule schedule) {
		Train train = trainDAO.findByNumber(schedule.getTrain()
				.getTrainNumber());
		if (train == null) {
			return "Invalid train information, no such train number in database";
		}
		Station station1 = stationDAO.findByName(schedule.getStation1()
				.getStationName());
		if (station1 == null) {
			return "Invalid departure station information, no such station name in database";
		}
		Station station2 = stationDAO.findByName(schedule.getStation2()
				.getStationName());
		if (station2 == null) {
			return "Invalid departure station information, no such station name in database";
		}
		scheduleDAO.delete(temp);
		List<Schedule> sched = scheduleDAO.findIsSchedulePossible(train, schedule.getDateOfDeparture(), schedule.getDateOfArrival());
		if (sched != null) {
			if (!sched.isEmpty()) {
					return "The train already has schedule in this time period";
				}
			
		}		
		temp.setTrain(train);
		temp.setStation1(station1);
		temp.setStation2(station2);
		temp.setDateOfArrival(schedule.getDateOfArrival());
		temp.setDateOfDeparture(schedule.getDateOfDeparture());
		scheduleDAO.save(temp);
		return "ok";
	}
}
